package banshee.ai;

import java.util.LinkedList;

import jp.ac.waseda.cs.washi.game.api.CharaInfo;
import jp.ac.waseda.cs.washi.game.api.GameInfo;
import jp.ac.waseda.cs.washi.game.api.MapInfo;
import jp.ac.waseda.cs.washi.game.api.PlayerAction;
import jp.ac.waseda.cs.washi.gameaiarena.api.Direction4;
import jp.ac.waseda.cs.washi.gameaiarena.api.Point2;

public class ActionFinder
{
	public LinkedList<PlayerAction>	actions;
	public CharaInfo				player;
	public MapInfo					map;
	public RawMap					rawMap;

	public ActionFinder(GameInfo gameInfo, RawMap rawMap,
			LinkedList<PlayerAction> actions)
	{
		this.actions = actions;
		player = gameInfo.getPlayer();
		map = gameInfo.getMap();
		this.rawMap = rawMap;
	}

	public void findComplate(Point2 p)
	{
		addRoute(p);
		spellTo4(p);
		actions.add(PlayerAction.NONE);
	}

	public void findCreateMini(Point2 p)
	{
//		System.out.println("findCreateMini"+p);
		addRoute(p);
		Point2 dest;
		spellTo4(p);
		dest = p.move(Direction4.UP);
		up();
		spellTo4(dest);
		dest = dest.move(Direction4.UP);
		if (map.isMovable(dest) && rawMap.spellCost0(dest) == -1) {
			up();
			clear();
			down();
		}
		dest = dest.move(Direction4.DOWN).move(Direction4.RIGHT);
		if (map.isMovable(dest) && rawMap.spellCost0(dest) == -1) {
			right();
			clear();
			down();
		} else {
			down();
			right();
		}
		dest = dest.move(Direction4.DOWN);
		spellTo4(dest);
		dest = dest.move(Direction4.RIGHT);
		if (map.isMovable(dest) && rawMap.spellCost0(dest) == -1) {
			right();
			clear();
			left();
		}
		dest = dest.move(Direction4.LEFT).move(Direction4.DOWN);
//		System.out.println(map.isMovable(dest));//
		if (map.isMovable(dest) && rawMap.spellCost0(dest) == -1) {
			down();
			clear();
			left();
		}else{
			left();
			down();
		}
		dest = dest.move(Direction4.LEFT);
		spellTo4(dest);
		dest = dest.move(Direction4.DOWN);
		if (map.isMovable(dest) && rawMap.spellCost0(dest) == -1) {
			down();
			clear();
			up();
		}
		dest = dest.move(Direction4.UP).move(Direction4.LEFT);
		if (map.isMovable(dest) && rawMap.spellCost0(dest) == -1) {
			left();
			clear();
			up();
		}else{
			up();
			left();
		}
		dest = dest.move(Direction4.UP);
		spellTo4(dest);
		dest = dest.move(Direction4.LEFT);
		if (map.isMovable(dest) && rawMap.spellCost0(dest) == -1) {
			left();
			clear();
			right();
		}
		dest = dest.move(Direction4.RIGHT).move(Direction4.UP);
		if (map.isMovable(dest) && rawMap.spellCost0(dest) == -1) {
			up();
			clear();
		}
		actions.add(PlayerAction.NONE);
	}

	private void addRoute(Point2 p)
	{
		Direction4 route[] = map.getPath(player.getLocation(), p);
//		System.out.println("route: "+player.getLocation()+p + route.length);
		for (Direction4 v : route) {
			actions.add(PlayerAction.fromDirection(v));
		}
	}

	private void spellTo4(Point2 p)
	{
		int spellTimes = rawMap.spellCost4(p);
		for (int i = 0; i < spellTimes; i++) {
			actions.add(PlayerAction.SPELL);
		}
	}

	private void up()
	{
		actions.add(PlayerAction.UP);
	}

	private void down()
	{
		actions.add(PlayerAction.DOWN);
	}

	private void left()
	{
		actions.add(PlayerAction.LEFT);
	}

	private void right()
	{
		actions.add(PlayerAction.RIGHT);
	}

	private void clear()
	{
		actions.add(PlayerAction.CLEAR);
	}
}
